﻿using Kingdee.BOS;
using Kingdee.BOS.App.Data;
using Kingdee.BOS.Core.CommonFilter.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Core.DynamicForm.PlugIn.ControlModel;
using Kingdee.BOS.Core.Metadata;
using Kingdee.BOS.Core.Permission;
using Kingdee.BOS.Core.SqlBuilder;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.ServiceHelper;
using Kingdee.BOS.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;

namespace RCE.K3.YGJD.Business.PlugIn.SAL.Report
{
    [Description("销售出库退货明细表-过滤条件")]
    [HotUpdate]
    public class SalInOutStockReportFilter : AbstractCommonFilterPlugIn
    {
        private List<long> lstOrg;
        #region 重写

        /// <summary>
        /// 树形节点单击事件
        /// </summary>
        /// <param name="e"></param>
        public override void TreeNodeClick(TreeNodeArgs e)
        {
            base.TreeNodeClick(e);
            this.InitSaleOrgId();
        }

        public override void OnInitialize(InitializeEventArgs e)
        {
            base.OnInitialize(e);

            //判断是否多组织
            if (this.View.ParentFormView != null && this.View.ParentFormView.BusinessInfo.GetForm().Id.Contains("_All"))
            {
            }
        }

        public override void AfterCreateNewData(EventArgs e)
        {
            base.AfterCreateNewData(e);
            DateTime startDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
            DateTime endDate = DateTime.Now;
            this.View.Model.SetValue("F_QAAW_StartDate", startDate);
            this.View.Model.SetValue("F_QAAW_EndDate", endDate);

            var enumList = getBillType(this.Context);
            var comboList = this.View.GetFieldEditor<ComboFieldEditor>("F_QAAW_BillType", 0);
            if (comboList != null)
            {
                comboList.SetComboItems(enumList);
            }
            this.View.Model.SetValue("F_QAAW_BillType", "销售出库单,销售退货单");
        }


        #endregion

        /// <summary>
        /// 初始化组织列表
        /// </summary>
        private void InitSaleOrgId()
        {
            if (this.View.ParentFormView != null)
            {
                this.lstOrg = this.GetPermissionOrg(this.View.ParentFormView.BillBusinessInfo.GetForm().Id);
            }
            List<EnumItem> organization = this.GetOrganization(Context);
            //获取控件编辑对象
            ComboFieldEditor fieldEditor = this.View.GetFieldEditor<ComboFieldEditor>("F_QAAW_SalOrgid", 0);
            //对控件进行赋值
            fieldEditor.SetComboItems(organization);

            this.Model.SetValue("F_QAAW_SalOrgid", this.Context.CurrentOrganizationInfo.ID);
        }

        /// <summary>
        /// 构造组织枚举项
        /// </summary>
        /// <param name="ctx"></param>
        /// <returns></returns>
        protected List<EnumItem> GetOrganization(Context ctx)
        {
            List<EnumItem> list = new List<EnumItem>();
            //设置查询字段
            List<SelectorItemInfo> lstSelectItem = new List<SelectorItemInfo>
            {
                new SelectorItemInfo("FORGID"),
                new SelectorItemInfo("FNUMBER"),
                new SelectorItemInfo("FNAME")
            };
            //设置过滤条件
            string text = this.GetInFilter("FORGID", this.lstOrg);
            text += string.Format(" AND FORGFUNCTIONS LIKE '%{0}%' ", 101L.ToString());
            //查询组织
            QueryBuilderParemeter para = new QueryBuilderParemeter
            {
                FormId = "ORG_Organizations",
                SelectItems = lstSelectItem,
                FilterClauseWihtKey = text
            };
            DynamicObjectCollection dynamicObjectCollection = QueryServiceHelper.GetDynamicObjectCollection(Context, para, null);
            foreach (DynamicObject current in dynamicObjectCollection)
            {
                list.Add(new EnumItem(new DynamicObject(EnumItem.EnumItemType))
                {
                    EnumId = current["FORGID"].ToString(),
                    Value = current["FORGID"].ToString(),
                    Caption = new LocaleValue(Convert.ToString(current["FName"]), Context.UserLocale.LCID)
                });
            }
            return list;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="formId"></param>
        /// <returns></returns>
        private List<long> GetPermissionOrg(string formId)
        {
            BusinessObject bizObject = new BusinessObject
            {
                Id = formId,
                PermissionControl = this.View.ParentFormView.BillBusinessInfo.GetForm().SupportPermissionControl,
                SubSystemId = this.View.ParentFormView.Model.SubSytemId
            };

            return PermissionServiceHelper.GetPermissionOrg(Context, bizObject, "6e44119a58cb4a8e86f6c385e14a17ad");
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        /// <param name="valList"></param>
        /// <returns></returns>
        private string GetInFilter(string key, List<long> valList)
        {
            if (valList == null || valList.IsEmpty<long>())
            {
                return string.Format("{0} = -1 ", key);
            }
            return string.Format("{0} in ({1})", key, string.Join<long>(",", valList));
        }


        /// <summary>

        /// 从数据库读取自定义数据源并转换成枚举项集合

        /// </summary>

        /// <param name="ctx"></param>

        /// <returns></returns>

        private List<EnumItem> getBillType(Context ctx)
        {
            var enumList = new List<EnumItem>();
            var enumItem = new EnumItem();
            enumItem.Caption = new LocaleValue("销售出库单");
            enumItem.EnumId = 0.ToString();
            enumItem.Invalid = false;
            enumItem.Value = "销售出库单";
            enumList.Add(enumItem);

            var enumItem1 = new EnumItem();
            enumItem1.Caption = new LocaleValue("销售退货单");
            enumItem1.EnumId = 1.ToString();
            enumItem1.Invalid = false;
            enumItem1.Value = "销售退货单";
            enumList.Add(enumItem1);

            return enumList;
        }

    }

}

